package com.shg.service.impl;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Service;

import com.shg.bean.BuyCart;
import com.shg.bean.BuyItem;
import com.shg.service.BuyCartService;

@Service
public class BuyCartServiceBean extends BasiceDaoImpl<BuyCart> implements BuyCartService {

	public void removeBook(String userid, String[] ids) {
		BuyCart buyCart = (BuyCart)sessionFactory.getCurrentSession().get(BuyCart.class, userid);
		buyCart.initList();
		//删除购物项
		for(String id:ids){
			BuyItem buyItem=new BuyItem();
			buyItem.setId(id);
			for(BuyItem item:buyCart.getItems()){
				if(item.equals(buyItem)){
					buyCart.getItems().remove(buyItem);
					break;
				}
			}
		}
		if(buyCart.getItems().size()<1){
			sessionFactory.getCurrentSession().delete(buyCart);
		}else{
			//重新构建Content
			String str=JSONArray.fromObject(buyCart.getItems()).toString();
			buyCart.setContent(str);
			//如果购物车里面有书籍就需要重新添加
			sessionFactory.getCurrentSession().update(buyCart);
		}
	}

}
